/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.business.PrivateManager;
import com.business.PublicManager;
import com.constants.UserPermissions;
import com.domain.Announcement;
import com.domain.Event;
import com.domain.User;
import com.domain.Profile;
import com.forms.LogInForm;
import com.forms.ProfileDateForm;
import com.forms.EditProfileForm;

public class LoginAction extends CommunityBaseAction{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);

		// Cast the actionForm, if there is one.
		ActionForward forward;
		LogInForm f = (LogInForm) form;
		
		PublicManager publicMgr = this.biz.getPublicMgr();
		PrivateManager privateMgr = this.biz.getPrivateMgr();
		
		//Everything to login a User
		User user = publicMgr.loginUser(f.getUsername(),f.getPassword());
		if(user!=null&&(user.isApproved()==true)){
			
			// instead calling populateAuthUserProfileForm
			
			this.populateAuthUserProfileForm(user,request);
			
			//request.getSession().setAttribute("AUTH_USER", user);
			//request.getSession().putValue("AUTH_USER", user);
			HttpSession session = request.getSession(true);
			session.setAttribute("AUTH_USER", user);
			
			if(user.getUserPermissions()==UserPermissions.ADMIN){
				request.getSession().setAttribute("PERMISSIONS", "admin");
			}
			else{
				request.getSession().setAttribute("PERMISSIONS", "public");
			}
			
			forward = mapping.findForward("success");
			
			//Now we need to get all the announcements and events to display
			List<Event> events = privateMgr.getTopEvents();
			List<Announcement> announcements = privateMgr.getTopAnnouncements();
			
			request.setAttribute("EVENTS", events);
			request.setAttribute("ANNOUNCEMENTS", announcements);
		}
		else{
			request.setAttribute("ERRORS", "Incorrect username or password");
			forward = mapping.findForward("failure");
		}
		
		return forward;
	}

	private void setAuthUsersProfileDates(Profile profile, HttpServletRequest request)
	{
		if (profile==null)
			return;
		
		
	 // set the user's profile dates like this perhaps? it works
			ProfileDateForm dateForm = new ProfileDateForm();
			if (profile.getEmploymentStartDate()!=null)
			{
				String date = profile.getEmploymentStartDate().toString();
				dateForm.setEmpStartYear(date.substring(0, 4));
				dateForm.setEmpStartMonth(date.substring(5, 7));
				dateForm.setEmpStartDay(date.substring(8, 10));
			}
			if (profile.getGraduationDate()!=null)
			{
				String date = profile.getGraduationDate().toString();				
				dateForm.setGradYear(date.substring(0, 4));
				dateForm.setGradMonth(date.substring(5, 7));
				dateForm.setGradDay(date.substring(8, 10));
			}
			
			//System.out.println("emp=" + dateForm.getEmpStartDay() + dateForm.getEmpStartMonth() + dateForm.getEmpStartYear()
			//		+ "grad=" + dateForm.getGradDay() + dateForm.getGradMonth() + dateForm.getGradYear());
				
			request.getSession().setAttribute("AUTH_USER_PROFILE_DATE_FORM", dateForm);
	}
	
	private void populateAuthUserProfileForm(User user, HttpServletRequest request)
	{
		Profile profile = user.getProfile();
		if (profile==null)
			return;		
				
		EditProfileForm f = new EditProfileForm();
		f.setAddress1(profile.getAddress1());
		f.setAddress2(profile.getAddress2());
		f.setCity(profile.getCity());
		f.setEducation(profile.getEducation());
		f.setEmployer(profile.getEmployer());
		if (profile.getEmploymentStartDate()!=null)
		{
			String date = profile.getEmploymentStartDate().toString();
			f.setEmpStartYear(date.substring(0, 4));
			f.setEmpStartMonth(date.substring(5, 7));
			f.setEmpStartDay(date.substring(8, 10));
		}
		f.setFirstName(profile.getFirstName());
		if (profile.getGraduationDate()!=null)
		{
			String date = profile.getGraduationDate().toString();				
			f.setGradYear(date.substring(0, 4));
			f.setGradMonth(date.substring(5, 7));
			f.setGradDay(date.substring(8, 10));
		}
		if (user.getUserPermissions().equals("admin"))
			f.setIsAdminUser("true");
		else
			f.setIsAdminUser("false");
		f.setLastName(profile.getLastName());
		f.setMajor(profile.getMajor());
		f.setPhone(profile.getPhone());
		f.setPositionDescription(profile.getPositionDescription());
		f.setProfessionalOrganizations(profile.getProfessionalOrganizations());
		if (profile.isPublicViewable())
			f.setPublicViewable("true");
		else
			f.setPublicViewable("false");
		f.setSecondaryEmail(profile.getSecondaryEmail());
		f.setState(profile.getState());
		f.setStudentOrganizations(profile.getStudentOrganizations());
		if (user.isSubscribedToEmails())
			f.setSubscribedToEmails("true");
		else
			f.setSubscribedToEmails("false");
		f.setZip(profile.getZip());
			
		
		//System.out.println("emp=" + dateForm.getEmpStartDay() + dateForm.getEmpStartMonth() + dateForm.getEmpStartYear()
		//		+ "grad=" + dateForm.getGradDay() + dateForm.getGradMonth() + dateForm.getGradYear());
				
		request.getSession().setAttribute("AUTH_USER_PROFILE_FORM", f);
	}
}
